
import random
import pymssql
from dbutils.pooled_db import PooledDB          #, SharedDBConnection
cnt = 100
i = 0

POOL = PooledDB(
    creator=pymssql,  # 使用链接数据库的模块
    maxconnections=0,  # 连接池允许的最大连接数，0和None表示不限制连接数
    mincached=3,  # 初始化时，链接池中至少创建的空闲的链接，0表示不创建
    maxcached=10,  # 链接池中最多闲置的链接，0和None不限制
    maxshared=0,  # 链接池中最多共享的链接数量，0和None表示全部共享。PS: 无用，因为pymysql和MySQLdb等模块的 threadsafety都为1，所有值无论设置为多少，_maxcached永远为0，所以永远是所有链接都共享。
    blocking=True,  # 连接池中如果没有可用连接后，是否阻塞等待。True，等待；False，不等待然后报错
    maxusage=0,  # 一个链接最多被重复使用的次数，None表示无限制
    setsession=[""],  # 开始会话前执行的命令列表。如：["set datestyle to ...", "set time zone ..."]set rowcount 10000
    ping=0,
    # ping MySQL服务端，检查是否服务可用。# 如：0 = None = never, 1 = default = whenever it is requested, 2 = when a cursor is created, 4 = when a query is executed, 7 = always
    host="127.0.0.1",
    port=1433,
    user="sa",
    password="Wardyan?*0400923",
    database="TESTDB",
    charset='utf8'
)

db = POOL.connection()
cursor = db.cursor()




alls = {}
keys = []
for a in range(1,cnt) :
    print(a)
    for b in range(1, cnt) :
        for c in range(1, cnt) :
            for operator1 in range(1, 5) :
                for operator2 in range(1, 5) :
                    # ## result = random.randint(100,9999)
                    # ## 第一个操作符 1+ 2- 3* 4/
                    # operator1 = random.randint(1,4)
                    # ## 第二个操作符  1+ 2- 3* 4/
                    # operator2 = random.randint(1,4)

                    sql = ""
                    ms = ""
                    ## 如果一个是乘除一个是加减， 确保把乘除放在前面
                    if operator2 > 2 and operator1 < 3 :
                        # t = operator2
                        # operator2 = operator1
                        # operator1 = t
                        ## 待处理：怎么把加减放在后面显示                        
                        s = ""
                        rs = 0

                        s += str(b)
                        
                        if operator2 == 1 :
                            s += " + " 
                            rs = b + c
                        elif operator2 == 2 :
                            s += " - "
                            rs = b - c
                        elif operator2 == 3 :
                            s += " × "
                            rs = b * c
                        elif operator2 == 4 :
                            if c == 0 :
                                continue
                            s += " ÷ " 
                            rs = b / c
                        s += str(c) + " = "
                        ## s = str(c) + s + " = "



                        ## s = str(a)
                        if operator1 == 1 :
                            s = " + " + s
                            rs = a + rs
                        elif operator1 == 2 :
                            s = " - " + s
                            rs = a - rs
                        elif operator1 == 3 :
                            s = " × " + s
                            rs = a * rs
                        elif operator1 == 4 :
                            if rs == 0 :
                                continue
                            s = " ÷ " + s
                            rs = a / rs
                        s = str(a) + s

                        if rs == int(rs) and rs >= 0 and rs < 10000:
                            ## 结果是INT 保存起来
                            ms = s
                            ## + str(rs)
                            ##keys.append(ms)
                            

                    else :
                        ## 按顺序来
                        s = ""
                        rs = 0
                        s = str(a)
                        if operator1 == 1 :
                            s += " + "
                            rs = a + b
                        elif operator1 == 2 :
                            s += " - "
                            rs = a - b
                        elif operator1 == 3 :
                            s += " × "
                            rs = a * b
                        elif operator1 == 4 :
                            s += " ÷ "
                            rs = a / b

                        s += str(b)
                        
                        if operator2 == 1 :
                            s += " + "
                            rs = rs + c
                        elif operator2 == 2 :
                            s += " - "
                            rs = rs - c
                        elif operator2 == 3 :
                            s += " × "
                            rs = rs * c
                        elif operator2 == 4 :
                            s += " ÷ "
                            rs = rs / c
                        s += str(c) + " = "
                        if rs == int(rs) and rs >= 0 and rs < 10000:
                            ## 结果是INT 保存起来
                            ## keys.append(s + str(rs))
                            ms = s
                            ##+ str(rs)
                            ##keys.append(ms)
                    if ms != "":
                        cursor.execute("insert into maths(xStr) values('" + ms + "')")
                        print("a=", a, "O1=", operator1, "b=", b, "O2:", operator2, "c=", c, "ms=", ms,"rs=", rs)
        db.commit()
                        

            

# # lines = []
# # for key in alls:
# #     ## print(key, alls[key])
# #     line = str(alls[key]) + str(key)
# #     lines.append(line)

# # f = open("H:\\DEV\\wpython\\math.txt", mode="w")
# # f.writelines(lines)
# # f.close()

# cnt = 1000
# i = 0
# oks = []
# while i < cnt :
#     line = random.choice(keys)
#     oks.append(line)
#     oks.append("\n")
#     i += 1

# f = open("H:\\DEV\\wpython\\mathoks.txt", mode="w")
# f.writelines(oks)
# f.close()


cursor.close()
db.close()
